package com.prp.gwt.client.widgets.user.profile;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.user.profile.ProfileListener;
import com.prp.gwt.client.vo.User;

public class ProfileView extends Composite {

	interface Binder extends UiBinder<Widget, ProfileView> {
	}

	private static final Binder binder = GWT.create(Binder.class);
	private static ProfileView instance;
	@UiField
	TextBox regName;
	@UiField
	Label regNameText;
	@UiField
	HTML regNameButton;
	@UiField
	HTML regNameCommit;
	@UiField
	HTML regNameCancel;
	@UiField
	public Label regNameMessage;
	@UiField
	public Image regNameLoadImage;
	@UiField
	TextBox realName;
	@UiField
	Label realNameText;
	@UiField
	HTML realNameButton;
	@UiField
	HTML realNameCommit;
	@UiField
	HTML realNameCancel;
	@UiField
	public Label realNameMessage;
	@UiField
	public Image realNameLoadImage;
	@UiField
	PortraitUploadPanel portraitUpload;
	@UiField
	Image portrait;
	@UiField
	HTML portraitButton;
	@UiField
	HTML portraitCommit;
	@UiField
	HTML portraitCancel;
	@UiField
	public Label portraitMessage;
	@UiField
	public Image portraitLoadImage;
	@UiField
	TextArea introduction;
	@UiField
	Label introductionText;
	@UiField
	HTML introductionButton;
	@UiField
	HTML introductionCommit;
	@UiField
	HTML introductionCancel;
	@UiField
	public Label introductionMessage;
	@UiField
	public Image introductionLoadImage;
	@UiField
	TextBox blog;
	@UiField
	Label blogText;
	@UiField
	HTML blogButton;
	@UiField
	HTML blogCommit;
	@UiField
	HTML blogCancel;
	@UiField
	public Label blogMessage;
	@UiField
	public Image blogLoadImage;
	@UiField
	PasswordPanel password;
	@UiField
	HTML passwordButton;
	@UiField
	HTML passwordCommit;
	@UiField
	HTML passwordCancel;
	@UiField
	public Label passwordMessage;
	@UiField
	public Image passwordLoadImage;
	@UiField
	public SimplePanel passwordEmpty;
	@UiField
	TextBox address;
	@UiField
	Label addressText;
	@UiField
	HTML addressButton;
	@UiField
	HTML addressCommit;
	@UiField
	HTML addressCancel;
	@UiField
	public Label addressMessage;
	@UiField
	public Image addressLoadImage;
	String loadImage = GWT.getModuleBaseURL() + "loading.gif";
	
	public static ProfileView getInstance() {
		if (instance == null)
			instance = new ProfileView();
		return instance;
	}

	public void load(User user) {
		regNameText.setText(user.getUsername());
		realNameText.setText(user.getRealname());
		portrait.setUrl(user.getPortrait());
		blogText.setText(user.getBlog());
		introductionText.setText(user.getIntroduction());
		addressText.setText(user.getAddress());
	}
	
	private ProfileView() {
		initWidget(binder.createAndBindUi(this));
		addStyleName("topPanel");
		{
			regNameButton.setHTML("<a>更改</a>");
			regNameCommit.setHTML("<a>提交</a>");
			regNameCancel.setHTML("<a>取消</a>");
			regNameLoadImage.setUrl(loadImage);
			regName.setMaxLength(16);
		}
		{
			realNameButton.setHTML("<a>更改</a>");
			realNameCommit.setHTML("<a>提交</a>");
			realNameCancel.setHTML("<a>取消</a>");
			realNameLoadImage.setUrl(loadImage);
			realName.setMaxLength(16);
		}
		{
			introductionButton.setHTML("<a>更改</a>");
			introductionCommit.setHTML("<a>提交</a>");
			introductionCancel.setHTML("<a>取消</a>");
			introductionLoadImage.setUrl(loadImage);
		}
		{
			blogButton.setHTML("<a>更改</a>");
			blogCommit.setHTML("<a>提交</a>");
			blogCancel.setHTML("<a>取消</a>");
			blogLoadImage.setUrl(loadImage);
			blog.setMaxLength(100);
		}
		{
			addressButton.setHTML("<a>更改</a>");
			addressCommit.setHTML("<a>提交</a>");
			addressCancel.setHTML("<a>取消</a>");
			addressLoadImage.setUrl(loadImage);
			address.setMaxLength(30);
		}
		{
			portraitButton.setHTML("<a>更改</a>");
			portraitCommit.setHTML("<a>上传</a>");
			portraitCancel.setHTML("<a>取消</a>");
			portraitLoadImage.setUrl(loadImage);
		}
		{
			passwordButton.setHTML("<a>更改</a>");
			passwordCommit.setHTML("<a>提交</a>");
			passwordCancel.setHTML("<a>取消</a>");
			passwordLoadImage.setUrl(loadImage);
		}
		restore();
		regNameLoadImage.setVisible(false);
		realNameLoadImage.setVisible(false);
		introductionLoadImage.setVisible(false);
		blogLoadImage.setVisible(false);
		addressLoadImage.setVisible(false);
		portraitLoadImage.setVisible(false);
		passwordLoadImage.setVisible(false);
	}

	public void restore() {
		{
			regNameButton.getParent().getParent().getParent().removeStyleName(
					"gwt-DecoratorPanel");
			regName.setVisible(false);
			regNameCommit.setVisible(false);
			regNameCancel.setVisible(false);
			regNameMessage.setText("");
			regNameButton.setVisible(true);
			regNameText.setVisible(true);
		}
		{
			realNameButton.getParent().getParent().getParent().removeStyleName(
					"gwt-DecoratorPanel");
			realName.setVisible(false);
			realNameCommit.setVisible(false);
			realNameCancel.setVisible(false);
			realNameMessage.setText("");
			realNameButton.setVisible(true);
			realNameText.setVisible(true);
		}
		{
			introductionButton.getParent().getParent().getParent().removeStyleName(
					"gwt-DecoratorPanel");
			introduction.setVisible(false);
			introductionCommit.setVisible(false);
			introductionCancel.setVisible(false);
			introductionMessage.setText("");
			introductionButton.setVisible(true);
			introductionText.setVisible(true);
			introduction.setVisibleLines(10);
		}
		{
			blogButton.getParent().getParent().getParent().removeStyleName(
					"gwt-DecoratorPanel");
			blog.setVisible(false);
			blogCommit.setVisible(false);
			blogCancel.setVisible(false);
			blogMessage.setText("");
			blogButton.setVisible(true);
			blogText.setVisible(true);
		}
		{
			address.getParent().getParent().getParent().removeStyleName("gwt-DecoratorPanel");
			address.setVisible(false);
			addressCommit.setVisible(false);
			addressCancel.setVisible(false);
			addressMessage.setText("");
			addressButton.setVisible(true);
			addressText.setVisible(true);
		}
		{
			portraitUpload.getParent().getParent().getParent().removeStyleName("gwt-DecoratorPanel");
			portraitUpload.setVisible(false);
			portraitCommit.setVisible(false);
			portraitCancel.setVisible(false);
			portraitMessage.setText("");
			portraitButton.setVisible(true);
			//portrait.setVisible(true);
			portrait.getParent().setVisible(true);
		}
		{
			password.getParent().getParent().getParent().removeStyleName("gwt-DecoratorPanel");
			password.setVisible(false);
			passwordCommit.setVisible(false);
			passwordCancel.setVisible(false);
			passwordMessage.setText("");
			passwordButton.setVisible(true);
			passwordEmpty.setVisible(true);
		}
	}

	@UiHandler("regNameButton")
	public void onRegNameButtonClick(ClickEvent event) {
		restore();
		regName.setText(regNameText.getText());
		regNameButton.setVisible(false);
		regNameText.setVisible(false);
		regName.setVisible(true);
		regNameCommit.setVisible(true);
		regNameCancel.setVisible(true);
		regNameButton.getParent().getParent().getParent()
				.addStyleName("gwt-DecoratorPanel");
		regNameMessage.setText("");
	}

	@UiHandler("regNameCommit")
	public void onRegNameCommitClick(ClickEvent event) {
		regNameLoadImage.setVisible(true);
		ProfileListener.getInstance().modifyRegName(regName.getText());
		restore();
	}

	@UiHandler("regNameCancel")
	public void onRegNameCancelClick(ClickEvent event) {
		restore();
	}

	@UiHandler("regName")
	public void onRegNameKeyUp(KeyUpEvent event){
		if(event.getNativeKeyCode() == 13){
			regNameLoadImage.setVisible(true);
			ProfileListener.getInstance().modifyRegName(regName.getText());
			restore();
		}
			
	}
	
	@UiHandler("realNameButton")
	public void onRealNameButtonClick(ClickEvent event) {
		restore();
		realName.setText(realNameText.getText());
		realNameButton.setVisible(false);
		realNameText.setVisible(false);
		realName.setVisible(true);
		realNameCommit.setVisible(true);
		realNameCancel.setVisible(true);
		realNameButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		realNameMessage.setText("");
	}

	@UiHandler("realNameCommit")
	public void onRealNameCommit(ClickEvent event) {
		realNameLoadImage.setVisible(true);
		ProfileListener.getInstance().modifyRealName(realName.getText());
		restore();
	}

	@UiHandler("realName")
	public void onRealNameKeyUp(KeyUpEvent event){
		if(event.getNativeKeyCode() == 13){
			realNameLoadImage.setVisible(true);
			ProfileListener.getInstance().modifyRealName(realName.getText());
			restore();
		}
	}
	
	@UiHandler("realNameCancel")
	public void onRealNameCancel(ClickEvent event) {
		restore();
	}

	@UiHandler("introductionButton")
	public void onIntroductionButtonClick(ClickEvent event) {
		restore();
		introduction.setText(introductionText.getText());
		introductionButton.setVisible(false);
		introductionText.setVisible(false);
		introduction.setVisible(true);
		introductionCommit.setVisible(true);
		introductionCancel.setVisible(true);
		introductionButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		introductionMessage.setText("");
	}

	@UiHandler("introductionCommit")
	public void onIntroductionCommitClick(ClickEvent event) {
		introductionLoadImage.setVisible(true);
		ProfileListener.getInstance().modifyIntroduction(introduction.getText());
		restore();
	}

	@UiHandler("introductionCancel")
	public void onIntroductionCancelClick(ClickEvent event) {
		restore();
	}

	@UiHandler("blogButton")
	public void onBlogButtonClick(ClickEvent event){
		restore();
		blog.setText(blogText.getText());
		blogButton.setVisible(false);
		blogText.setVisible(false);
		blog.setVisible(true);
		blogCommit.setVisible(true);
		blogCancel.setVisible(true);
		blogButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		blogMessage.setText("");
	}
	
	@UiHandler("blogCommit")
	public void onBlogCommit(ClickEvent event){
		blogLoadImage.setVisible(true);
		ProfileListener.getInstance().modifyBlog(blog.getText());
		restore();
	}
	
	@UiHandler("blog")
	public void onBlogKeyUpHandler(KeyUpEvent event){
		if(event.getNativeKeyCode() == 13){
			blogLoadImage.setVisible(true);
			ProfileListener.getInstance().modifyBlog(blog.getText());
			restore();
		}
	}
	
	@UiHandler("blogCancel")
	public void onBlogCancel(ClickEvent event){
		restore();
	}
	
	@UiHandler("addressButton")
	public void onAddressButtonClick(ClickEvent event){
		restore();
		address.setText(addressText.getText());
		addressButton.setVisible(false);
		addressText.setVisible(false);
		address.setVisible(true);
		addressCommit.setVisible(true);
		addressCancel.setVisible(true);
		addressButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		addressMessage.setText("");
	}
	
	@UiHandler("addressCommit")
	public void onAddressCommit(ClickEvent event){
		addressLoadImage.setVisible(true);
		ProfileListener.getInstance().modifyAddress(address.getText());
		restore();
	}
	
	@UiHandler("address")
	public void onAddressKeyUp(KeyUpEvent event){
		if(event.getNativeKeyCode() == 13){
			addressLoadImage.setVisible(true);
			ProfileListener.getInstance().modifyAddress(address.getText());
			restore();
		}
	}
	
	@UiHandler("addressCancel")
	public void onAddressCancel(ClickEvent event){
		restore();
	}
	
	@UiHandler("portraitButton")
	public void onPortraitButtonClick(ClickEvent event){
		restore();
		portraitUpload.setImage(portrait.getUrl());
		portraitButton.setVisible(false);
		//portrait.setVisible(false);
		portrait.getParent().setVisible(false);
		portraitUpload.setVisible(true);
		portraitCommit.setVisible(true);
		portraitCancel.setVisible(true);
		portraitButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		portraitMessage.setText("");
	}
	
	@UiHandler("portraitCommit")
	public void onPortraitCommitClick(ClickEvent event){
		if(portraitUpload.upload()){
			portraitLoadImage.setVisible(true);
			restore();
		}
	}
	
	@UiHandler("portraitCancel")
	public void onPortraitCancelClick(ClickEvent event){
		restore();
	}
	
	@UiHandler("passwordButton")
	public void onPasswordButtonClick(ClickEvent event){
		restore();
		password.reset();
		passwordButton.setVisible(false);
		password.setVisible(true);
		passwordCommit.setVisible(true);
		passwordCancel.setVisible(true);
		passwordEmpty.setVisible(false);
		passwordButton.getParent().getParent().getParent().addStyleName(
				"gwt-DecoratorPanel");
		passwordMessage.setText("");
	}
	
	@UiHandler("passwordCommit")
	public void onPasswordCommitClick(ClickEvent event){
		if(password.commit()){
			restore();
			passwordLoadImage.setVisible(true);
		}
	}

	@UiHandler("passwordCancel")
	public void onPasswordCancelClick(ClickEvent event){
		restore();
	}
	
}
